//
//  DiscoverSearchView.swift
//  WeiboProject
//
//  Created by amos on 16/1/18.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

class DiscoverSearchView: UIView {

    @IBOutlet weak var searchFieldConstraint: NSLayoutConstraint!
    @IBOutlet weak var searchField: UITextField!
    @IBOutlet weak var cancleBtn: UIButton!
    
    class func searchView() ->  DiscoverSearchView{
        return NSBundle.mainBundle().loadNibNamed("DiscoverSearchView", owner: nil, options: nil).last as! DiscoverSearchView
    }
    
    // MARK: - 哈哈
    @IBAction func searchCancleClick(sender: UIButton) {
        
        //更新约束
        searchFieldConstraint.constant = 0
        searchField.updateConstraintsIfNeeded()
        UIView.animateWithDuration(0.5) { () -> Void in
            
          self.searchField.layoutIfNeeded()
            
         //   self.searchField.frame.size.width = self.frame.width
            self.searchField.resignFirstResponder()
        }
        
    }
    
    @IBAction func searchFieldBeginEditing(sender: AnyObject) {
        
        
        searchFieldConstraint.constant = 30
        searchField.updateConstraintsIfNeeded()
        UIView.animateWithDuration(0.5) { () -> Void in
              self.searchField.layoutIfNeeded()
           // self.searchField.becomeFirstResponder()
                //self.searchField.frame.size.width = self.frame.width - 30
        }
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        
        searchField.leftView!.frame.size.height = searchField.frame.height
        searchField.leftView!.frame.size.width = searchField.frame.height
    }
    

    override func awakeFromNib() {
        //1.设置左搜索图片
        let iamge:UIImage = UIImage(named: "searchbar_textfield_search_icon")!
        
        let imageView: UIImageView = UIImageView(image: iamge)
        
 
        
        //2.设置图片显示模式---中间
        imageView.contentMode = .Center
        
        searchField.leftView = imageView
        
        //设置显示模式--要不然不会显示
        searchField.leftViewMode = .Always
    }


}
